const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
const dbHelper = require('db-helper')
module.exports = class OrderController extends Controller {
	async confirmSendTicket() {
		let {
			orderId
		} = this.ctx.data
		if (!orderId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			await this.service.order.confirmSendTicket({
				orderId
			})
			return {
				code: 200
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async confirmChargeoffed() {
		let {
			orderId,
			usedCount,
			overdueCount
		} = this.ctx.data
		if (!orderId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			await this.service.order.confirmChargeoffed({
				orderId,
				usedCount,
				overdueCount
			})
			return {
				code: 200
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getChargeoffReceiptList(){
		let {
			ticketId,
			storeId,
			pageNum,
			pageSize,
			startDate,
			endDate,
			name
		} = this.ctx.data
		try{
			let params = {
				ticketId,
				storeId,
				status: 1,
				startDate,
				endDate,
				name,
				pageNum,
				pageSize
			}
			let isEmployee = this.ctx.auth.role.some(role => {return role === 'employee'})
			if (isEmployee) {
				params.employeeId = this.ctx.auth.uid
			}
			let page = await this.service.order.getReceiptList(params)
			return {
				code: 200,
				result: page
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getReceiptList(){
		let {
			ticketId,
			storeId,
			status,
			pageNum,
			pageSize
		} = this.ctx.data
		try{
			let page = await this.service.order.getReceiptList({
				ticketId,
				storeId,
				status,
				pageNum,
				pageSize
			})
			return {
				code: 200,
				result: page
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getRefundApplyDetail() {
		let {
			applyId
		} = this.ctx.data
		if (!applyId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			let order = await this.service.order.getRefundApplyDetail({
				applyId
			})
			return {
				code: 200,
				result: order
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async getOrderList() {
		let {
			pageNum,
			pageSize,
			status,
			keyword,
			channel,
			city,
			orderType = 'goods'
		} = this.ctx.data
		try{
			let result = {
				code: 200,
				result: {
					total: 0,
					list: []
				}
			}
			if (orderType == 'goods') {
				result.result = await this.service.order.getGoodsOrderList({
					pageNum,
					pageSize,
					status,
					keyword,
					channel,
					city
				})
			} else if (orderType == 'subscribe') {
				result.result = await this.service.order.getSubscribeOrderList({
					pageNum,
					pageSize,
					status,
					keyword,
					channel,
					city
				})
			}
			return result
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async prolongOrder() {
		let {
			orderId,
			targetDate
		} = this.ctx.data
		if (!orderId || !parseInt(targetDate)) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			let order = await this.service.order.prolongOrder({
				orderId,
				targetDate
			})
			return {
				code: 200,
				result: order
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getOrderStatistics() {
		let {
			city = '4201'
		} = this.ctx.data
		let isRegionMgr = this.ctx.auth.role.some(role => {
			return role === 'region_mgr'
		})
		if (isRegionMgr) {
			let userRes = await this.db.collection("uni-id-users").doc(this.ctx.auth.uid)
				.field({
					city: true
				})
				.get()
			city = userRes.data[0].city
		}
		try{
			let result = await this.service.order.getOrderStatistics({
				city
			})
			return {
				code: 200,
				result
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	async getTicketInfo() {
		let {
			codeId
		} = this.ctx.data
		if (!codeId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			let res = await this.service.order.getTicketInfo({
				codeId
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: 500,
				result: e.message
			}
		}
	}
	async getReceiptInfo() {
		let {
			codeId
		} = this.ctx.data
		if (!codeId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			let res = await this.service.order.getReceiptInfo({
				codeId
			})
			return {
				code: 200,
				result: res
			}
		}catch(e){
			return {
				code: e.message == '无权限' ? 403 : 500,
				msg: e.message
			}
		}
	}
	async chargeoffTicket() {
		let {
			codeId,
			storeId
		} = this.ctx.data
		if (!codeId || !storeId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		return this.service.order.chargeoffTicket({
			codeId, storeId
		})
	}
	
	async getTicketChargeoffRecords() {
		let {
			storeId,
			pageNum = 1,
			pageSize = 20,
			startDate,
			endDate
		} = this.ctx.data
		if (!storeId) {
			return {
				code: 500,
				msg: '门店id不能为空'
			}
		}
		return this.service.order.getTicketChargeoffRecords({
			storeId,
			pageNum,
			pageSize,
			startDate,
			endDate
		})
	}
	
	async auditRefund() {
		const {
			applyId,
			fullRefund, //是否全额退款
			damages, //因为商家原因而退款使平台造成的损失由商家承担，此为金额
			rejected,
			rejectReason
		} = this.ctx.data
		if (!applyId || (damages && (isNaN(parseInt(damages)) || parseInt(damages) < 0)) || (rejected && !rejectReason)) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try{
			let apply = await this.service.order.auditRefund({
				applyId,
				fullRefund,
				damages,
				rejected,
				rejectReason
			})
			return {
				code: 200,
				result: apply
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	async refund() {
		const {
			orderId,
			reason,
			fullRefund, //是否全额退款
			damages, //因为商家原因而退款使平台造成的损失由商家承担，此为金额
			count
		} = this.ctx.data
		if (!orderId || !reason || (damages && isNaN(parseInt(damages)))) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		return this.service.order.refund({
			orderId,
			reason,
			fullRefund,
			damages,
			count
		})
	}
	//核销
	async chargeoffReceipt(){
		let{
			codeId,
			storeId,
			count
		} = this.ctx.data
		if (!codeId || !storeId || !count || !parseInt(count)) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try{
			await this.service.order.chargeoffReceipt({
				codeId, storeId, count: parseInt(count)
			})
			return {
				code: 200
			}
		}catch(e){
			return {
				code: 500,
				msg: e.message
			}
		}
		return 
	}
}